<?php

namespace common\models\elasticsearch\entity;

use common\models\c2\query\Query;
use cza\base\models\ActiveRecord;
use cza\base\models\statics\EntityModelStatus;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "{{%article}}".
 *
 * @property integer $id
 * @property string $title
 * @property string $description
 * @property string $content
 * @property integer $created_by
 * @property integer $updated_by
 * @property integer $status
 * @property string $created_at
 * @property string $updated_at
 */
class Article extends \yii\elasticsearch\ActiveRecord
{

    public static function index()
    {
        return "es_article_index";
    }

    //创建的类型(注:在6.x以上的版本一个索引现在只有一个类型，以下可以创建多个类型)
    public static function type()
    {
        return 'article';
    }

    # mapping配置
    public static function mapConfig(){
        return [
            'properties' => [
                'id'  => ['type' => 'long',  "index" => "not_analyzed"],
                'title'      => ['type' => 'string',  "index" => "not_analyzed"],
                'description'      => ['type' => 'string',  "index" => "not_analyzed"],
                'content'      => ['type' => 'string',  "index" => "not_analyzed"],
                'created_by'      => ['type' => 'long',  "index" => "not_analyzed"],
                'updated_by'      => ['type' => 'long',  "index" => "not_analyzed"],
                'status'      => ['type' => 'long',  "index" => "not_analyzed"],
                'created_at'  => ['type' => 'date',  "index" => "not_analyzed"],
                'updated_at'  => ['type' => 'date',  "index" => "not_analyzed"],
            ]
        ];
    }

    public static function mapping()
    {
        return [
            static::type() => self::mapConfig(),
        ];
    }

    /**
     * Set (update) mappings for this model
     */
    public static function updateMapping()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->setMapping(static::index(), static::type(), static::mapping());
    }

    public static function getMapping(){
        $db = self::getDb();
        $command = $db->createCommand();
        return $command->getMapping();
    }

    /**
     * Create this model's index
     */
    public static function createIndex()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->createIndex(static::index(), [
            //'settings' => [ /* ... */ ],
            'mappings' => static::mapping(),
            //'warmers' => [ /* ... */ ],
            //'aliases' => [ /* ... */ ],
            //'creation_date' => '...'
        ]);
    }

    /**
     * Delete this model's index
     */
    public static function deleteIndex()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->deleteIndex(static::index(), static::type());
    }

    /**
     * @return array the list of attributes for this record
     */
    public function attributes()
    {
        // path mapping for '_id' is setup to field 'id'
        // return ['id', 'title', 'description', 'content', 'created_by', 'updated_by',
        //     'status', 'created_at', 'updated_at'];
        $mapConfig = self::mapConfig();
        return array_keys($mapConfig['properties']);
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
                'value' => function () {
                    return date('Y-m-d H:i:s');
                },
            ],
        ];
    }

    /**
     * @param $query Query
     */
    public static function active($query)
    {
        $query->andWhere(['status' => EntityModelStatus::STATUS_ACTIVE]);
    }

    //为索引的各个字段设置类型(必须都要设置),更多的es字段类型请参考es手册的mapping部分
    // public static function mapConfig()
    // {
    //     return [
    //         'properties' => [
    //             'id' => ['type' => 'long'],
    //             'article_attributor' => [
    //                 'type' => 'nested', //nested类型,相当于mysql当中的表与表之间的关系，子文档包含在父文档当中
    //                 'properties' => [
    //                     'attr_id' => ['type' => 'long', "index" => "not_analyzed"],//如果不想让es进行分词等操作，只是想当成一个和数据库类似的搜索，因此，mapping的定义为：”index” => “not_analyzed”
    //                     'name' => ['type' => 'text', 'analyzer' => 'ik_max_word'] //与settings中设置的中文分词进行关联
    //                 ]
    //             ],
    //         ]
    //     ];
    // }

    //模型中的rulse规则，注：unique定义时慎重，会导致数据不能导入，因为unique会被转义为es当中的filter导致导入时报错
    // public function rules()
    // {
    //     return [
    //         ['id']
    //     ];
    // }

}